<template>
  <div class="app-container">
    <el-form :rules="rules" ref="form" :model="form" label-width="90px" style="width:300px;">
      <el-form-item label="原始密码" prop="password">
        <el-input v-model="form.password" class="test"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="passwordNew">
        <el-input v-model="form.passwordNew" type="password" autocomplete="off" auto-complete="new-password"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="accountPwdTwo">
        <el-input v-model="form.accountPwdTwo" type="password" autocomplete="off" auto-complete="new-password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="onSubmit" :loading="btnLoading">确定</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { passwordUpdate } from '@/api/account'
export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能小于6位'))
      } else {
        callback()
      }
    }
    const validatePass1 = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error('请确认密码'))
      } else {
        if (this.form.passwordNew !== this.form.accountPwdTwo) {
          callback(new Error('输入密码不一致'))
        } else {
          callback()
        }
      }
    }
    return {
      btnLoading: false,
      form: {
        password: '',
        passwordNew: '',
        accountPwdTwo: '',
        id: null
      },
      rules: {
        password: [{ required: true, message: '请填写原始密码', trigger: 'change' }],
        passwordNew: [{ required: true, trigger: 'change', validator: validatePass }],
        accountPwdTwo: [{ required: true, trigger: 'change', validator: validatePass1 }]
      }
    }
  },
  computed: {
    ...mapGetters([
      'token',
      'name'
    ])
  },
  created() {
    this.form.id = this.token
    this.form.userName = this.name
  },
  methods: {
    onSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.btnLoading = true
          passwordUpdate(this.form).then(response => {
            this.btnLoading = false
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          }).catch(() => {
            this.btnLoading = false
          })
        }
      })
    },
    onCancel() {
      this.$refs['form'].clearValidate()
    }
  }
}
</script>

<style scoped>
</style>